import React, { Component } from "react";
import { Card, message, Select } from "antd";
import { ProTable } from "@ant-design/pro-components";
import { getOrderList, updateStatus } from "./service";

class CartCmt extends Component {
  state = {
    dataSource: [],
  };
  columns = [
    {
      title: "姓名",
      dataIndex: "name",
      key: "name",
    },
    {
      title: "商品",
      dataIndex: "proname",
      width: 350,
      key: "proname",
    },
    {
      title: "住址",
      dataIndex: "province",
      key: "province",
      hideInSearch: true,
    },
    {
      title: "订单状态",
      render: (row) => {
        const { Option } = Select;
        return (
          <>
            <Select
              defaultValue={row.status}
              //   defaultValue={()=>{

              //   }}
              onChange={(e) => {
                // console.log(this);
                this.handleChange( e, row.orderid);
              }}
            >
              <Option value={0}>未支付</Option>
              <Option value={1}>待发货</Option>
              <Option value={2}>待收货</Option>
              <Option value={3}>代评价</Option>
              <Option value={4}>已完成</Option>
            </Select>
          </>
        );
      },
    },
    {
      title: "手机号",
      dataIndex: "tel",
      key: "tel",
    },
  ];
  render() {
    return (
      <>
        <Card title="订单">
          <ProTable
            rowKey="orderid"
            dataSource={this.state.dataSource}
            // request =
            columns={this.columns}
            pagination={{ defaultPageSize: 8 }}
          ></ProTable>
        </Card>
      </>
    );
  }

  //获取订单数据列表
  getData = async () => {
    const res = await getOrderList();
    if (!res.code) return message.error("查询失败");
    message.success(res.message);
    let { data } = res;
    data = data.filter((item) => item.name !== "");
    this.setState({ dataSource: data });
    console.log(data);
  };
  //修改订单中的订单状态
  handleChange = async(e, id) => {
    console.log(id,e);
    const res =await updateStatus(id,e)
    if(!res.code) return message.error("修改出错")
    message.success(res.message)
  };
  componentDidMount() {
    this.getData();
  }
}

export default CartCmt;
